package com.lyshop.yinying.fragments;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lyshop.yinying.R;
import com.lyshop.yinying.activitys.FundRecordActivity;
import com.lyshop.yinying.activitys.HeaderSelectActivity;
import com.lyshop.yinying.activitys.PointsMallActivity;
import com.lyshop.yinying.activitys.RetailOrderActivity;
import com.lyshop.yinying.activitys.TakeMoneyActivity;
import com.lyshop.yinying.activitys.TakeOrderActivity;
import com.lyshop.yinying.activitys.TicketActivity;
import com.lyshop.yinying.activitys.VIPCenterActivity;
import com.lyshop.yinying.base.BaseActivity;
import com.lyshop.yinying.base.BaseFragment;
import com.lyshop.yinying.bean.GifBean;
import com.lyshop.yinying.bean.UserInfoBean;
import com.lyshop.yinying.constants.Constant;
import com.lyshop.yinying.eventbus.EventCenter;
import com.lyshop.yinying.eventbus.EventConstants;
import com.lyshop.yinying.utils.CustomerUtil;
import com.lyshop.yinying.utils.DataUtils;
import com.lyshop.yinying.utils.GlideUtils;
import com.lyshop.yinying.utils.RouteUtil;
import com.lyshop.yinying.utils.ToastUtil;
import com.lyshop.yinying.view.CertificateModule;
import com.lyshop.yinying.view.PromptDialog;
import com.lyshop.yinying.view.ToSetPayPassDialog;

public class MineFragment extends BaseFragment implements View.OnClickListener{
    private static final String TAG = "个人中心";
    ImageView iv_header,iv_level,iv_great_people,iv_mian_gif_bg, iv_refresh_money;
    TextView tv_nick_name,tv_integral,tv_voucher,tv_money,tv_money_out,tv_money_join;
    LinearLayout ll_money_record,ll_xsxt,ll_lypj,ll_sckf,ll_zxkf,ll_lsdd,ll_thdd,ll_bbjc,ll_gywm,ll_seting, ll_red_bag, ll_turn_card;
    LinearLayout ll_integral_mall,ll_voucher;
    RelativeLayout rl_level;
    private ObjectAnimator rotateAnimator;
    @Override
    protected void initVariable() {
        if (DataUtils.isLogin(mContext)) {
            UserInfoBean bean = DataUtils.getUserInfo(mContext);
            if (bean.is_great_people) {
                iv_great_people.setVisibility(View.VISIBLE);
            } else {
                iv_great_people.setVisibility(View.GONE);
            }
            tv_nick_name.setText(bean.nickname);
            tv_integral.setText(bean.integral + "");
            tv_money.setText(bean.availablebalance + "");
            tv_voucher.setText(bean.pro_ticket_money_sum + "");
            String levelimgname = "level_" + bean.level;
            int resID = getResources().getIdentifier(levelimgname, "drawable", "com.yx.snshop");
            iv_level.setImageResource(resID);
            GlideUtils.showImageOptions(mContext, bean.header, iv_header);
            if (bean.money_sum > 0) {
                iv_mian_gif_bg.setVisibility(View.GONE);
            } else {
                iv_mian_gif_bg.setVisibility(View.VISIBLE);
            }
        } else {
            iv_mian_gif_bg.setVisibility(View.VISIBLE);
            iv_great_people.setVisibility(View.GONE);
            tv_nick_name.setText("登录/注册");
            tv_integral.setText("0");
            tv_money.setText("0");
            tv_voucher.setText("0");
            iv_level.setImageResource(R.drawable.level_1);
            int resID = getResources().getIdentifier("mrtx", "drawable", "com.yx.snshop");
            iv_header.setImageResource(resID);
        }
        GifBean gifBean = myApplication.getGifMap().get("2");
        if (gifBean == null || TextUtils.isEmpty(gifBean.img)) {
            iv_mian_gif_bg.setVisibility(View.GONE);
        } else {
            GlideUtils.showImageFitxyOptions(mContext, gifBean.img, iv_mian_gif_bg);
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void findViews(View rootView) {
        iv_header =  (ImageView) $(R.id.iv_main_header);
        iv_level = (ImageView) $(R.id.mine_level_img);
        iv_great_people = $(R.id.iv_great_people);
        iv_mian_gif_bg = $(R.id.iv_mian_gif_bg);
        tv_nick_name = (TextView) $(R.id.tv_nick_name);
        tv_integral = (TextView) $(R.id.mine_integral_text);
        tv_voucher = (TextView) $(R.id.mine_voucher_text);
        tv_money = (TextView) $(R.id.mine_money_text);
        ll_money_record  = (LinearLayout) $(R.id.mine_money_record);
        tv_money_join = (TextView) $(R.id.mine_money_join);
        tv_money_out = (TextView) $(R.id.mine_money_out);
        ll_xsxt = (LinearLayout) $(R.id.mine_xsxt);
        ll_lypj = (LinearLayout) $(R.id.mine_lypj);
        ll_zxkf = (LinearLayout) $(R.id.mine_zxkf);
        ll_sckf = (LinearLayout) $(R.id.mine_sckf);
        ll_lsdd = (LinearLayout) $(R.id.mine_lsdd);
        ll_thdd = (LinearLayout) $(R.id.mine_thdd);
        ll_bbjc = (LinearLayout) $(R.id.mine_bbjc);
        ll_gywm = (LinearLayout) $(R.id.mine_gywm);
        ll_red_bag = (LinearLayout) $(R.id.ll_red_bag);
        ll_turn_card = (LinearLayout) $(R.id.ll_turn_card);
        iv_refresh_money = (ImageView) $(R.id.iv_refresh_money);

        ll_seting = (LinearLayout) $(R.id.mine_shezhi);
        ll_integral_mall = (LinearLayout) $(R.id.mine_integral_mall);
        ll_voucher = (LinearLayout) $(R.id.mine_voucher);
        rl_level =(RelativeLayout) $(R.id.mine_level);

        // 初始化属性动画：旋转两圈（720度）
        rotateAnimator = ObjectAnimator.ofFloat(iv_refresh_money, "rotation", 0f, 720f);
        rotateAnimator.setDuration(2000); // 总时长2秒（每圈1秒）
        rotateAnimator.setInterpolator(new LinearInterpolator()); // 匀速旋转
        rotateAnimator.setRepeatCount(0); // 不重复（直接转两圈）
    }

    @Override
    protected void setListeners() {
        iv_header.setOnClickListener(this);
        tv_nick_name.setOnClickListener(this);
        ll_integral_mall.setOnClickListener(this);
        ll_voucher.setOnClickListener(this);
        rl_level.setOnClickListener(this);
        ll_money_record.setOnClickListener(this);
        tv_money_join.setOnClickListener(this);
        tv_money_out.setOnClickListener(this);
        iv_mian_gif_bg.setOnClickListener(this);
        ll_xsxt.setOnClickListener(this);
        ll_lypj.setOnClickListener(this);
        ll_sckf.setOnClickListener(this);
        ll_zxkf.setOnClickListener(this);
        ll_lsdd.setOnClickListener(this);
        ll_thdd.setOnClickListener(this);
        ll_bbjc.setOnClickListener(this);
        ll_gywm.setOnClickListener(this);
        ll_seting.setOnClickListener(this);
        ll_red_bag.setOnClickListener(this);
        ll_turn_card.setOnClickListener(this);
        iv_refresh_money.setOnClickListener(this);
        // 动画结束监听
        rotateAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                refreshDataSource(); // 动画结束后刷新余额
            }
        });
    }

    @Override
    protected void refreshDataSource() {
        if (DataUtils.isLogin(mContext)) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    myApplication.updateUserDetail();
                }
            }, 500);

        }else {
            initVariable();
        }
    }

    @Override
    public void onClick(View view) {

        if(view.getId() == R.id.mine_gywm){
            RouteUtil.toWebViewActivity(baseActivity, Constant.H5_ABOUT_US);
            return;
        }
        if(view.getId() == R.id.mine_zxkf){
            CustomerUtil.customerService(mContext);
            return;
        }
        if(view.getId() == R.id.mine_sckf){
            goCallCustomService();
            return;
        }

        if (!DataUtils.isLogin(mContext)) {
            ((BaseActivity) mContext).onLogoutStatus();
            return;
        }
        if(view.getId() == R.id.mine_money_out){
            if (CertificateModule.getInstance(mContext).isCertificate(null)) {
                if (DataUtils.getIsSetPayPass(mContext)){
                    gotoActivity(mContext, TakeMoneyActivity.class, null);
                }else {
                    ToSetPayPassDialog toSetPayPassDialog = new ToSetPayPassDialog(mContext);
                    if (!toSetPayPassDialog.isShowing()){
                        toSetPayPassDialog.show();
                    }
                }
            }
            return;
        }
        if(view.getId() == R.id.mine_money_join){
            RouteUtil.toRechargeActivity((BaseActivity) mContext);
            return;
        }
        if(view.getId() == R.id.mine_integral_mall){
            gotoActivity(mContext, PointsMallActivity.class, null);
            return;
        }
        if(view.getId() == R.id.mine_lsdd){
            gotoActivity(mContext, RetailOrderActivity.class, null);
            return;
        }
        if(view.getId() == R.id.mine_thdd){
            gotoActivity(mContext, TakeOrderActivity.class, null);
            return;
        }
        if(view.getId() == R.id.mine_lypj){
            RouteUtil.toWebViewActivity(baseActivity, Constant.H5_EVALUATE);
            return;
        }
        if(view.getId() == R.id.mine_xsxt){
            RouteUtil.toInvestSchoolNewActivity(baseActivity);
            return;
        }
        if(view.getId() == R.id.mine_bbjc){
            ToastUtil.showToast(mContext,"当前为最新版本");
            return;
        }
        if (view.getId() == R.id.mine_shezhi){
            RouteUtil.toSettingActivity(baseActivity);
            return;
        }
        if(view.getId() == R.id.mine_money_record){
            gotoActivity(mContext, FundRecordActivity.class, null);
            return;
        }
        if (view.getId() == R.id.mine_level){
            gotoActivity(mContext, VIPCenterActivity.class, null);
            return;
        }
        if(view.getId() == R.id.mine_voucher){
            gotoActivity(mContext, TicketActivity.class, null);
            return;
        }
        if (view.getId() == R.id.iv_main_header){
                gotoActivity(mContext, HeaderSelectActivity.class, null);
            return;
        }
        if(view.getId() ==R.id.iv_mian_gif_bg){
            RouteUtil.toRechargeActivity((BaseActivity) mContext);
            return;
        }

        if (view.getId() == R.id.ll_red_bag){
            if (isUserValidLogin()) {
                RouteUtil.toRedRainActivity((BaseActivity) mContext);
            }
            return;
        }

        if (view.getId() == R.id.ll_turn_card){
            if (isUserValidLogin()) {
                RouteUtil.toTurnCardActivity((BaseActivity) mContext);
            }
        }

        if (view.getId() == R.id.iv_refresh_money) {
            if (!rotateAnimator.isRunning()) {
                rotateAnimator.start(); // 开始动画
            }
            return;
        }
    }

    private PromptDialog callDialog;

    private void goCallCustomService() {
        String content = "商城客服：" + "400-606-8287" + "<br/>工作日：9:00-12:00 14:00-17:00";
        if (null == callDialog) {
            callDialog = new PromptDialog.Builder(mContext)
                    .setIcon(false)
                    .setTitle("商城客服")
                    .setContent(content)
                    .setButton("取消", "拨打")
                    .setCancelAble(true)
                    .setGravity(Gravity.CENTER)
                    .setOnConfirmListener(new PromptDialog.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            //跳转到拨号界面，同时传递电话号码
                            String number = "4006068287";
                            Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
                            startActivity(dialIntent);
                        }
                    }).show();
        } else {
            if (!callDialog.isShowing()) {
                callDialog.show();
            }
        }

    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {
        super.onEventComming(eventCenter);
        int event = eventCenter.getEventCode();
        switch (event) {
            case EventConstants.EVENTCODE_UPDATE_USER:
                initVariable();
                break;
        }
    }
}
